package com.summer.threadSwitch;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class ThreadSwitch {
	
	public static void main(String[] args) {
		
		ThreadSwitch threadSwitch = new ThreadSwitch();
		System.out.println("主线程名：="+Thread.currentThread().getName());
		threadSwitch.addSyn(()->{
			System.out.println(" 线程名：="+Thread.currentThread().getName());
			return "任务1";
		}).addAsyn(()->{
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(" 线程名：="+Thread.currentThread().getName());
			return "任务2";
		}).addSyn(()->{
			System.out.println(" 线程名：="+Thread.currentThread().getName());
			return "任务3";
		});
		
		threadSwitch.addAsyn(()->{
			System.out.println(" 线程名：="+Thread.currentThread().getName());
			return "任务4";
		});
		
		threadSwitch.addAsyn(()->{
			System.out.println(" 线程名：="+Thread.currentThread().getName());
			return "任务5";
		});
		threadSwitch.run();
	}
	
	public ArrayList<Future<?>> futureList = new ArrayList<>();
	
	public ThreadSwitch addSyn(Func<?> func){
		futureList.add(SummerThreadFactory.factory.buildSyn(func));
		return this;
	}
	
	public ThreadSwitch addAsyn(Func<?> func){
		futureList.add(SummerThreadFactory.factory.buildAsyn(func));
		return this;
	}
	
	public void run(){
		for(Future<?> future: futureList){
			
			try {
				String msg = (String) future.get();
				System.out.println("msg ==="+msg);
			} catch (InterruptedException | ExecutionException e) {
				e.printStackTrace();
			}

		}
	}
	
	interface Func<R>{
		R call();
	}
	
	
}
